﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SqlServerDataExporter
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Connect(object sender, RoutedEventArgs e)
        {           
            try
            {
                ViewModel.Connect();
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Error");
            }
        }

        private void Button_ExportData(object sender, RoutedEventArgs e)
        {          
            bool? file = this.radioButtonFile.IsChecked;
            try
            {
                ViewModel.StartExport(file);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Error");
            }
        }

        /// <summary>
        /// Select the file where to write the insertstatemetns into.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_SelectFile(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.FileName = "export.sql"; // Default file name
            dlg.DefaultExt = ".sql"; // Default file extension
            dlg.Filter = "Sql documents (.sql)|*.sql"; // Filter files by extension

            // Show save file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process save file dialog box results
            if (result == true)
            {
                // Save document
                string filename = dlg.FileName;
                ViewModel.FileName = filename;
            }
        }

        /// <summary>
        /// This is for the tool tab (convert hex to binary).
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_SaveHexStringAsBinary(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            
            // Show save file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            if (result == true)
            {
                // Save document
                string filename = dlg.FileName;
                try
                {
                    ViewModel.SaveHexStringAsBinary(filename);
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message, "Error");
                }

            }
        }

        /// <summary>
        /// This is for the tool tab (convert hex to binary).
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_LoadBinaryAsHexString(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

            // Show save file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            if (result == true)
            {
                // Save document
                string filename = dlg.FileName;
                try
                {
                    ViewModel.LoadBinaryAsHexString(filename);
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message, "Error");
                }

            }
        }

        private void Button_NextTab(object sender, RoutedEventArgs e)
        {
            ViewModel.NextTab();
        }

        private void Button_CreateConnectionString(object sender, RoutedEventArgs e)
        {
            ViewModel.CreateConnectionString();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            ViewModel.SaveSettings();
        }

        private ViewModel ViewModel
        {
            get 
            { 
                ViewModel model = (ViewModel)this.FindResource("viewModel"); 
                return model; 
            }

        }
    }
}
